<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('PdkPHPMailerSender')) return;
/**
 * SPIP Plugin Development Kit
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage pdk
 * @category   outils
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/Developmet-Kit
 * @version    1.0.0
 */

include_spip('classes/pdk_model');
/** 
 *  This is mail sender based on PHPMailer class
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
class PdkPHPMailerSender {
    /**
     *  Sends message via PHPMailer
     *  @param array or string $email_to Array of emails, or single email
     *  @param string $subject
     *  @param string $message
     *  @param array $options Array of additional parameters wich can have following elements: 
     *               from_name - Sender name 
     *               from_email - Sender email
     *               attach_files_data - array of data files to attach
     */
    public function Send($email_to, $subject, $message, $options = array()) {
        if (!is_array($email_to) && $email_to)
            $email_to = array($email_to);           
        if (!is_array($email_to) || !$email_to) 
            return false;
             
        $from_name = $options['from_name'] ? $options['from_name'] : '';
        $from_email = $options['from_email'] ? $options['from_email'] : '';
        
        include_spip('classes/phpmailer/class.phpmailer');
        
        $ok = false;
        foreach ($email_to as $email) {
            $mail = new PHPMailer();
            $mail->CharSet = 'utf8';
            $mail->Subject = "=?UTF-8?B?" . base64_encode($subject)."?=";
            if ($from_email)
                $mail->SetFrom($from_email, "=?UTF-8?B?" . base64_encode(typo($from_name))."?=");
            $mail->AddAddress($email);
            $mail->IsHTML(true);
            $mail->Body = $message;
            
        
        if (isset($options['attach_files_data']) && !empty($options['attach_files_data'])) {
            foreach ($options['attach_files_data'] as $file_data) {
                $mail->AddAttachment($file_data['full_path']);                    
            }
        }
        
            $ok = (bool)$mail->Send();
        }
        
        return $ok;
    }
}
?>